package objectio;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Queue;


class Producer implements Runnable {
	private Queue<Person> q;
	private BufferedReader reader;
	
	public Producer(Queue<Person> q,BufferedReader reader) {
		super();
		this.q = q;
		this.reader = reader;
	}

	@Override
	public void run() {
		System.out.println("producer start..............");
		String line = null;
		Person person = null;
		
		try {
			while((line = reader.readLine()) != null){
				person = stringToObject(line);
				synchronized (q) {
					q.add(person);
					System.out.println("produce......" + person.toString() + q.size());
					q.notify();
/*					try {
						Thread.sleep(1);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}*/
					if(q.size() >= 10 ){
						try {
							q.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
			Main.flag = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Person stringToObject(String str){
		Person person = new Person();
		String[] msg = null;
		msg = str.split("\t");
		person.setId(Integer.valueOf(msg[0]));
		person.setName(msg[1]);
		person.setAge(Integer.valueOf(msg[2]));
		return person;
	}
}